package be.xios.ti.droodle;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

/**
 * Het tekenprogramma
 * 
 * @author Kris Hermans
 */
public class DroodleApp {
	private JFrame basicFrame;

	private JPanel content;

	private ShapeJPanel shapePanel;

	private JToolBar toolbar;

	private JLabel statusLabel;

	//toolbar buttons
	private JButton openButton;

	private JButton saveButton;

	private JButton lineButton;

	private JButton circleButton;

	private JButton circleFillButton;

	private JButton rectangleButton;

	private JButton rectangleFillButton;

	private JButton fgButton;

	private JLabel fgLabel;

	private JLabel bgLabel;

	private JButton bgButton;

	private JComboBox sizeBox;

	private void buildGui() {
		setLF();
		basicFrame = new JFrame("Droodle: een xml tekenprogramma.");
		content = new JPanel();
		content.setLayout(new BorderLayout());

		statusLabel = new JLabel(" ", JLabel.RIGHT);
		statusLabel.setBorder(BorderFactory.createEtchedBorder());
		content.add(statusLabel, BorderLayout.PAGE_END);

		Integer[] model = new Integer[10];

		for (int i = 1; i <= 10; i++) {
			model[i - 1] = new Integer(i);
		}
		sizeBox = new JComboBox(model);

		shapePanel = new ShapeJPanel(statusLabel, sizeBox);
		shapePanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
		content.add(shapePanel, BorderLayout.CENTER);

		toolbar = new JToolBar();
		toolbar.setRollover(true);
		toolbar.setFloatable(false);
		addButtons();
		content.add(toolbar, BorderLayout.PAGE_START);

		basicFrame.setContentPane(content);
		basicFrame.setSize(500, 500);
		basicFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		basicFrame.setVisible(true);
	}

	private void setLF() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void addButtons() {
		toolbar.addSeparator();

		openButton = createButton("Open24.gif", "Load from xml file");
		toolbar.add(openButton);
		openButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				JFileChooser fc = new JFileChooser();
				int ok = fc.showOpenDialog(basicFrame);
				if (ok == JFileChooser.APPROVE_OPTION) {
					File file = fc.getSelectedFile();
					Drawing d = XMLFormat.load(file);
					shapePanel.setDrawing(d);
				}
				
			}
		
		});

		saveButton = createButton("Save24.gif", "Save to xml file");
		toolbar.add(saveButton);
		saveButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				JFileChooser fc = new JFileChooser();
				int ok = fc.showSaveDialog(basicFrame);
				if (ok == JFileChooser.APPROVE_OPTION) {
					File file = fc.getSelectedFile();
					XMLFormat.save(file, shapePanel.getDrawing());
				}
				
			}
			
		});
		toolbar.addSeparator();

		lineButton = createButton("line.png", "Line");
		toolbar.add(lineButton);
		lineButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				shapePanel.setCurrentAction(new LineDrawAction(shapePanel));

			}

		});

		circleButton = createButton("circle.png", "Circle");
		toolbar.add(circleButton);
		circleButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				shapePanel.setCurrentAction(new CircleDrawAction(shapePanel));
			}

		});

		circleFillButton = createButton("circlefill.png", "Filled Circle");
		toolbar.add(circleFillButton);
		circleFillButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				shapePanel.setCurrentAction(new FillCircleDrawAction(shapePanel));
			}

		});

		rectangleButton = createButton("rectangle.png", "Rectangle");
		toolbar.add(rectangleButton);
		rectangleButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				shapePanel.setCurrentAction(new RectangleShapeAction(shapePanel));

			}

		});

		rectangleFillButton = createButton("rectangle-fill.png",
				"Filled Rectangle");
		toolbar.add(rectangleFillButton);
		rectangleFillButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				shapePanel.setCurrentAction(new FillRectangleShapeAction(shapePanel));

			}

		});

		toolbar.addSeparator();

		fgButton = createButton("fg.png", "Foreground Color");
		toolbar.add(fgButton);
		fgButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				Color c = chooseColor(fgLabel.getBackground());
				fgLabel.setBackground(c);
				shapePanel.setForeground(c);
			}

		});

		fgLabel = new JLabel("     ");
		fgLabel.setOpaque(true);
		fgLabel.setBackground(shapePanel.getForeground());
		toolbar.add(fgLabel);

		bgButton = createButton("bg.png", "Background Color");
		toolbar.add(bgButton);
		bgButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				Color c = chooseColor(bgLabel.getBackground());
				bgLabel.setBackground(c);
				shapePanel.setBackground(c);
			}

		});

		bgLabel = new JLabel("     ");
		bgLabel.setOpaque(true);
		bgLabel.setBackground(shapePanel.getBackground());
		toolbar.add(bgLabel);

		toolbar.addSeparator();
		toolbar.add(new JLabel("pendikte"));

		toolbar.add(sizeBox);
	}

	private Color chooseColor(Color c) {
		return JColorChooser.showDialog(basicFrame, "Kies een kleur", c);
	}

	private JButton createButton(String img, String tooltip) {
		ClassLoader cl = this.getClass().getClassLoader();
		String path = "be/xios/ti/droodle/icons/";

		JButton btn = new JButton(new ImageIcon(cl.getResource(path + img)));
		btn.setToolTipText(tooltip);

		return btn;
	}

	public static void main(String[] args) {
		DroodleApp app = new DroodleApp();
		app.buildGui();

	}
}